﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ext.Net.Utilities;
using Ext.Net;
using MemberMgr.Data.BusinessObjects;
using MemberMgr.Data.ManagerObjects;
using MemberMgr.Website.Common;

namespace MemberMgr.Website
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (X.IsAjaxRequest)
            {

            }

            if (!this.IsPostBack)
            {
                HttpCookie cook = this.Request.Cookies["deptno"];
                ItShopManager shopMgr = CommonUtil.managerFactory.GettShopManager();

                IList<tShop> list = shopMgr.GetAll();

                foreach (tShop item in list)
                {
                    Ext.Net.ListItem temp = new Ext.Net.ListItem(item.Shopname, item.Id.ToString());

                    this.cmbShop.Items.Add(temp);
                }

                if (this.cmbShop.Items.Count > 0)
                {
                    if (cook != null)
                    {
                        this.cmbShop.SelectedIndex = this.cmbShop.Items.IndexOf(this.cmbShop.Items.Where(p => p.Value == cook.Value).FirstOrDefault());
                    }
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Ext.Net.MessageBox temp = new MessageBox();
            ItUserManager userManager = CommonUtil.managerFactory.GettUserManager();

            IList<tUser> list = userManager.GetUserInfoByLoginIDAndDept(this.txtUsername.Text.Trim(),uint.Parse(this.cmbShop.SelectedItem.Value));

            if (list != null && list.Count > 0)
            {
                if (list.First().Password == this.txtPassword.Text.Trim())
                {
                    //Set Cookie
                    HttpCookie cookie = new HttpCookie(CommonConst.COOKIE_DEPT);
                    cookie.Name = "deptno";
                    cookie.Value = list.First().TShop.Id.ToString();
                    cookie.Expires = DateTime.Now.AddMonths(3);
                    this.Response.Cookies.Add(cookie);

                    
                    this.Session[CommonConst.SESSION_USERINFO] = list.First();
                    X.Redirect("~/Default.aspx", "登录成功,正在跳转...");

                    
                }
                else
                {
                    temp.Show(new MessageBoxConfig()
                    {
                        Title = "登录失败",
                        Message = "密码错误，请确认后重试!",
                        Icon = MessageBox.Icon.ERROR,
                        HeaderIcon = Icon.Error,
                        Buttons = MessageBox.Button.OK
                    });
                }
            }
            else
            {
                temp.Show(new MessageBoxConfig()
                {
                    Title = "登录失败",
                    Message = "用户名错误，没有当前登录用户，请确认后重试!",
                    Icon = MessageBox.Icon.ERROR,
                    HeaderIcon = Icon.Error,
                    Buttons = MessageBox.Button.OK
                });

            }

        }
    }
}
